﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Barebones.Components;
using Barebones.Dependencies;
using Meat.Resources;

namespace Meat.Resources
{

    public abstract class AbstractHandle : EntityComponent
    {
        public abstract String Name
        { get; }

        public abstract T GetAbstract<T>() where T : class;
    }

    /// <summary>
    /// Represents a handle to a resource, used to assign resources to entities
    /// </summary>
    /// <typeparam name="T">The resource type this handle is linked to ie: Model, VertexBufferResource etc.</typeparam>
    public class Handle<T> : AbstractHandle where T : class
    {
        IResource m_Resource;
        String m_ResourceName;

        public override string Name
        {
            get { return m_ResourceName; }
        }

        public Handle(String resourceName)
        {
            m_ResourceName = resourceName;
        }

        public override IEnumerable<IDependency> GetDependencies()
        { 
            yield return new Dependency<ResourceLoader>(OnResourceLoaderBinded);
        }

        public T Get()
        {
            //todo return null if not loaded or something to support dynamic loading and unloading
            return m_Resource.Get<T>();
        }

        public override ResT GetAbstract<ResT>()
        {
            return m_Resource.Get<ResT>();
        }

        void OnResourceLoaderBinded(ResourceLoader loader)
        {
            m_Resource = loader.GetResource(m_ResourceName);
        }
    }
}
